<?php
namespace Api\Controller;
use Think\Controller;
class WithdrawController extends Controller{
    //获取提现页面信息
    public function getInfo()
    {
        $data_p = I('post.');
        $wid = $data_p['wid'];
        $map['wid'] = $wid;
        $map['shop_id'] = $data_p['shop_id'];
        //商家个人信息
        $shop = M("Shop")->where($map)->find();
        //系统设置，手续费
        $wechat = M('wechat')->where(array('wid'=>$wid))->find();
        $list['shop'] = $shop;
        $list['set'] = $wechat;
        $arr = array(
            "err_code"=>0,
            "info"=> $list
        );
        $this->ajaxReturn($arr);
    }
    //提交申请
    public function submit()
    {
        $data_p = I('post.');
        $wid = $data_p['wid'];
        $map['wid'] = $wid;
        $map['shop_id'] = $data_p['shop_id'];
        $shop = M("Shop")->where($map)->find();
        $wechat = M('wechat')->where(array('wid'=>$wid))->find();
        if(!$shop){
          $arr = array(
              "err_code"=>400,
              "err_msg"=>"商家不存在！"
          );
          $this->ajaxReturn($arr);
        }else{
            if(!$data_p['name'] || $data_p['name']=="undefined"){
                $arr = array(
                    "err_code"=>400,
                    "err_msg"=>"请填写姓名！"
                );
                $this->ajaxReturn($arr);
            }
            $apply_money = $data_p['money'];
            if(!$shop['money'] || $shop['money']<0 ||$shop['money'] =="undefined"){
                $arr = array(
                    "err_code"=>400,
                    "err_msg"=>"请输入提现金额！"
                );
                $this->ajaxReturn($arr);
            }
            if($apply_money > $shop['money']){
                $arr = array(
                    "err_code"=>400,
                    "err_msg"=>"提现金额超出余额！"
                );
                $this->ajaxReturn($arr);
            }
            if($data_p['type'] == 1 && (!$data_p['wx'] || $data_p['wx']=="undefined")){
                $arr = array(
                    "err_code"=>400,
                    "err_msg"=>"请输入微信号！"
                );
                $this->ajaxReturn($arr);
            }
            if($data_p['type'] == 2){
                if(!$data_p['name'] || $data_p['name']=="undefined"){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"请填写姓名！"
                    );
                    $this->ajaxReturn($arr);
                }
                if(!$data_p['alipay'] || $data_p['alipay']=="undefined"){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"请填写支付宝帐号！"
                    );
                    $this->ajaxReturn($arr);
                }
                //var_dump($data_p);exit;
                if($data_p['alipay'] != $data_p['alipay1']){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"两次填写的支付宝不一致！"
                    );
                    $this->ajaxReturn($arr);
                }
            }


            if($data_p['type'] == 3){
                if(!$data_p['name'] || $data_p['name']=="undefined"){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"请填写姓名！"
                    );
                    $this->ajaxReturn($arr);
                }
                if(!$data_p['bankname'] || $data_p['bankname']=="undefined"){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"请输入银行！"
                    );
                    $this->ajaxReturn($arr);
                }
                if(!$data_p['bankcard'] || $data_p['bankcard']=="undefined"){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"请填写银行卡号！"
                    );
                    $this->ajaxReturn($arr);
                }
                if($data_p['bankcard'] != $data_p['bankcard1']){
                    $arr = array(
                        "err_code"=>400,
                        "err_msg"=>"两次填写的银行卡号不一致！"
                    );
                    $this->ajaxReturn($arr);
                }
            }

            switch($data_p['type']){
                //微信
                case 1:
                    $data['wx'] = $data_p['wx'];
                    break;
                //支付宝
                case 2:
                    $data['name'] = $data_p['name'];
                    $data['alipay'] = $data_p['alipay'];
                    break;
                //银行卡
                case 3:
                    $data['name'] = $data_p['name'];
                    $data['bankname'] = $data_p['bankname'];
                    $data['bankcard'] = $data_p['bankcard'];
            }
            $data['rate'] = $wechat['withdraw_rate'];
            $data['deduct_money'] = $apply_money * ($wechat['withdraw_rate']/100);
            $data['real_money'] = $apply_money - $data['deduct_money'];
            $data['wid'] = $wid;
            $data['status'] = 0;
            $data['type'] = $data_p['type'];
            $data['name'] = $data_p['name'];
            $data['shop_id'] = $data_p['shop_id'];
            $data['apply_money'] = $apply_money;
            $data['createtime'] = date("Y-m-d H:i:s",time());

            $r[] = M('withdraw')->add($data);
            $r[] = M('Shop')->where($map)->setDec('money',$apply_money);
            if(!in_array(false,$r)){
                $arr = array(
                    "err_code"=>0,
                    "err_msg"=>"申请成功！"
                );
                $this->ajaxReturn($arr);
            }
        }

    }
    //提现记录
    public function getlist()
    {
        $map['wid'] = I('wid');
        $map['shop_id'] = I('shop_id');
        $list = M('withdraw')->where($map)->order('createtime desc')->select();
        if($list){
            $arr = array(
                "err_code"=>0,
                "list"=>$list
            );
            $this->ajaxReturn($arr);
        }else{
            $arr = array(
                "err_code"=>400,
                "list"=>'暂无数据'
            );
            $this->ajaxReturn($arr);
        }
    }
}